package com.ca.ui.core.adapter;

import android.annotation.SuppressLint;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ui.core.R;
import com.ca.ui.core.bean.MessageInfo;
import com.ca.ui.core.util.ImageUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageHolder> {

    private List<MessageInfo> list;

    public MessageAdapter(List<MessageInfo> list) {
        this.list = list == null ? new ArrayList<>() : list;
    }

    @NonNull
    @Override
    public MessageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_item_layout, parent, false);
        return new MessageHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageHolder holder, int position) {

        MessageInfo info = list.get(position);

        ImageUtils.loadCircleImage(holder.headerView.getContext(), info.getHeaderUrl(), holder.headerView);
        holder.nameView.setText(info.getName());
        holder.timeView.setText(info.getTime());
        holder.contentView.setText(info.getContent());

        if (info.getUnReadCount() != 0) {
            holder.badge.setBadgeNumber(info.getUnReadCount());
        }

        //  点击事件
        holder.parent.setOnClickListener(v -> {});
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void setList(List<MessageInfo> list) {

        if (list == null) {
            list = new ArrayList<>();
        }

        list.sort((o1, o2) -> o2.getTime().compareTo(o1.getTime()));

        if (!this.list.equals(list)) {
            this.list = list;
            notifyDataSetChanged();
        }
    }

    public static class MessageHolder extends RecyclerView.ViewHolder {

        public ConstraintLayout parent;
        public ImageView headerView;
        public TextView nameView;
        public TextView contentView;
        public TextView timeView;

        public Badge badge;
        public FrameLayout fl;
        public TextView dotTargetView;

        public MessageHolder(@NonNull View itemView) {
            super(itemView);
            parent = itemView.findViewById(R.id.cl_parent);
            headerView = itemView.findViewById(R.id.iv_header);
            nameView = itemView.findViewById(R.id.tv_name);
            contentView = itemView.findViewById(R.id.tv_content);
            timeView = itemView.findViewById(R.id.tv_time);

            fl = itemView.findViewById(R.id.fl);
            dotTargetView = itemView.findViewById(R.id.dot_target);

            badge = new QBadgeView(itemView.getContext())
                    .bindTarget(dotTargetView)
                    .setBadgeTextSize(8, true)
                    .setExactMode(false)
                    .setShowShadow(true)
                    .setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
                        @Override
                        public void onDragStateChanged(int i, Badge badge, View view) {

                        }
                    });
        }
    }
}
